<template>
  <el-row class="seal-manage-module">
    <el-col class="title">设计需求发布</el-col>
    <el-col class="main-content">
      <el-tabs v-model="activeName">
        <el-tab-pane label="加工需求发布" name="1">
          <el-form
            ref="publishForm"
            :model="publishForm"
            :rules="rules"
            label-width="100px"
            label-position="left"
          >
            <el-card shadow="always" class="mb-10">
              <div slot="header">基本信息</div>
              <el-form-item label="需求标题" prop="demandTitle">
                <el-input
                  v-model="publishForm.demandTitle"
                  placeholder="请填写作品标题"
                  style="width: 220px;"
                ></el-input>
              </el-form-item>
              <el-form-item label="需求类目" prop="demandCategory">
                <el-select
                  v-model="publishForm.demandCategory"
                  placeholder="请填写商品类目"
                  style="width: 220px;"
                >
                  <el-option
                    v-for="item in publishForm.categoryList"
                    :key="item.value"
                    :value="item.value"
                    :label="item.label"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-card>
            <el-card shadow="always" class="mb-10">
              <div slot="header">产品图片</div>
              <el-form-item label="产品" prop="product">
                <el-row type="flex">
                  <el-col :span="8">
                    <el-upload
                      class="product-uploader"
                      :action="actionUrl"
                      :show-file-list="false"
                      :on-success="uploadSuccess"
                      :before-upload="beforeUpload"
                    >
                      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                      <i v-else class="el-icon-plus product-uploader-icon"></i>
                    </el-upload>
                  </el-col>
                  <el-col>
                    <div>
                      <ul class="upload-limit">
                        <li>1.图片大小建议不超过 3M;</li>
                        <li>2.图片尺寸 700*700px;</li>
                        <li>3.图片比例 1:1</li>
                      </ul>
                    </div>
                  </el-col>
                </el-row>
              </el-form-item>
              <el-form-item label="参考链接" prop="link" style="width: 320px;">
                <el-input v-model="publishForm.link" placeholder="参考链接"></el-input>
              </el-form-item>
              <el-form-item label="设计需求" prop="demand" style="width: 780px;">
                <el-input
                  type="textarea"
                  :autosize="{ minRows: 2, maxRows: 4}"
                  resize="none"
                  placeholder="请输入"
                  v-model="publishForm.demand"
                ></el-input>
              </el-form-item>
            </el-card>
            <el-card shadow="always" class="mb-10">
              <div slot="header">数量和价格</div>
              <el-form-item class="custom-label" label="设计类型" prop="type">
                <el-checkbox-group v-model="publishForm.type">
                  <el-checkbox label="3D 渲染图" name="3d"></el-checkbox>
                  <el-checkbox label="电商素材" name="ds"></el-checkbox>
                  <el-checkbox label="尺寸图" value="cc" name="cc"></el-checkbox>
                </el-checkbox-group>
              </el-form-item>
              <el-row>
                <el-col :span="5">
                  <el-form-item label="价格范围" prop="price1">
                    <el-input
                      v-model="publishForm.price1"
                      style="width: 120px;"
                      oninput="value=value.replace(/[^\d.]/g,'')"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="1">
                  <span style="display: inline-block;width:50px;text-align:center;">~</span>
                </el-col>
                <el-col :span="4">
                  <el-form-item prop="price2" label-width="30px">
                    <el-input
                      v-model="publishForm.price2"
                      style="width: 120px;"
                      oninput="value=value.replace(/[^\d.]/g,'')"
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-card>
            <el-card shadow="always" class="mb-10">
              <div slot="header">时间</div>
              <el-form-item label="交货时间">
                <el-date-picker type="date" placeholder="选择日期" v-model="publishForm.date"></el-date-picker>
              </el-form-item>
            </el-card>
            <el-form-item>
              <el-button @click="draftSave">保存草稿</el-button>
              <el-button type="primary" @click="draftPublish">立即发布</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="设计需求发布" name="2"></el-tab-pane>
      </el-tabs>
    </el-col>
  </el-row>
</template>
<script>
import { getProductMaterial } from "@/api";
export default {
  name: "productManage",
  data() {
    const validPrice1 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入价格下限"));
      } else {
        if (this.publishForm.price2 !== "") {
          this.$refs.publishForm.validateField("price2");
        }
        callback();
      }
    };
    const validPrice2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入价格上限"));
      } else if (value < this.publishForm.price1) {
        callback(new Error("价格上限不能小于下限"));
      } else {
        callback();
      }
    };
    return {
      activeName: "1",
      publishForm: {
        demandTitle: "",
        demandCategory: "",
        categoryList: [
          { label: "category1", value: 1 },
          { label: "category2", value: 2 }
        ],
        link: "",
        demand: "",
        type: ['尺寸图','3D 渲染图'],
        price1: "",
        price2: "",
        date: "",
        product: ""
      },
      rules: {
        demandTitle: [{ required: true, message: "必填", trigger: "blur" }],
        demandCategory: [
          { required: true, message: "必选", trigger: "change" }
        ],
        type: [{ required: true, message: "必选", trigger: "change" }],
        price1: [{ validator: validPrice1, trigger: "blur" }],
        price2: [{ validator: validPrice2, trigger: "blur" }],
        product: [{ required: true, message: "请上传图片", trigger: "submit" }]
      },
      actionUrl: "https://jsonplaceholder.typicode.com/posts/",
      imageUrl: ""
    };
  },
  methods: {
    beforeUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt3M = file.size / 1024 / 1024 < 3;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt3M) {
        this.$message.error("上传头像图片大小不能超过 3MB!");
      }
      return isJPG && isLt3M;
    },
    uploadSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    validateDraft(cb) {
      this.$refs.publishForm.validate(valid => {
        if (valid) {
          cb();
        }
      });
    },
    save() {
      console.log("save");
    },
    draftSave() {
      this.validateDraft(this.save);
    },
    draftPublish() {}
  }
};
</script>
<style lang="scss" scoped>
.custom-label {
  /deep/.el-form-item__label {
    &::after {
      display: block;
      content: "(可多选)";
      margin-left: 15px;
      font-size: 12px;
      color: #cccccc;
    }
  }
}
.product-uploader {
  display: inline-block;
  /deep/.el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;

    &:hover {
      border-color: #409eff;
    }

    .product-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }
  }
}
li {
  list-style: none;
}
.upload-limit {
  padding-left: 5px;
  width: 200px;
  border: 1px solid #e6e6e6;
  color: #e6e6e6;
}
.mb-10 {
  margin-bottom: 10px;
}
.clearfix {
  &:after {
    visibility: hidden;
    display: block;
    font-size: 0;
    content: " ";
    clear: both;
    height: 0;
  }
}
.fl {
  float: left;
}
.fr {
  float: right;
}
.seal-manage-module {
  .title {
    margin-bottom: 20px;
    padding: 0 10px;
    height: 60px;
    line-height: 60px;
    box-shadow: 0px 2px 4px 0px #d1d9e2;
  }
  .main-content {
    background-color: #fff;
    padding: 10px;

    .seal {
      text-align: center;
      font-size: 16px;
      .card-title {
        color: #4d4d4d;
        font-weight: 500;
        margin: 10px 0;
      }
      .card-content {
        color: #808080;
        font-weight: 400;
        margin-bottom: 20px;
      }
    }
    .table {
      margin: 10px 0;
      .delete-btn {
        color: red;
      }
    }
  }
}
</style>